package com.cxx.food.controller;

import com.cxx.food.dto.file.ChunkInfoDto;
import com.cxx.food.dto.file.ChunkResultDto;
import com.cxx.food.dto.file.FileInfoDto;
import com.cxx.food.dto.file.FileRecordDto;
import com.cxx.food.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/file")
@Tag(name = "文件管理")
public class FileController {
    @Resource
    private FileService fileService;

    @Operation(summary = "上传文件")
    @PostMapping(value = "", headers = "content-type=multipart/form-data")
    public FileRecordDto uploadFile(@RequestPart("file") MultipartFile file,
                                    @RequestParam("path") String path,
                                    @RequestParam("md5") String md5) {
        return fileService.uploadFile(file, path, md5);
    }

    @Operation(summary = "上传文件块")
    @PostMapping("/chunk")
    public Boolean uploadChunk(ChunkInfoDto chunkInfo,
                               String path) {
        return fileService.uploadChunk(chunkInfo, path);
    }

    @Operation(summary = "验证当前文件块是否上传")
    @GetMapping("/chunk")
    public ChunkResultDto checkChunk(@RequestParam String identifier,
                                     @RequestParam String filename,
                                     @RequestParam String path) {
        return fileService.checkChunk(identifier, filename, path);
    }

    @Operation(summary = "删除当前已上传的文件块")
    @DeleteMapping("/chunk")
    public Boolean deleteChunk(@RequestParam String identifier,
                               @RequestParam String path) {
        return fileService.deleteChunk(identifier, path);
    }

    @Operation(summary = "合并文件")
    @PostMapping("/chunk/merge")
    public FileRecordDto mergeFile(@RequestParam String filename,
                                   @RequestParam String path) {
        return fileService.mergeFile(filename, path);
    }

    @Operation(summary = "获取文件夹目录")
    @GetMapping("/catalog")
    public List<FileInfoDto> getFolderInfo(@RequestParam String folderName) {
        return fileService.getFolderInfo(folderName);
    }
}